//
// Created by voyagerhui on 2022/3/18.
//

#include "read_write.h"

void ReadWriteClass::parse_row(string s, int format) {
    char str[1000] = {0};
    for(int i=0; i<s.size(); i++) str[i] = s[i];  // string to char[]

    if(format == qos_row_1){   // qos.csv row 1
        char *pch = strtok(str, ",");  // 丢弃字符site_name
        pch = strtok(NULL, ",");
        while(pch != NULL){
            client_name.push_back(pch);  // 保存qos中client名字序列
            pch = strtok(NULL, ",");
        }
    }else if(format == qos_row_2){   // qos.csv row 2-end
        vector<int> row_data;
        char *pch = strtok(str, ",");
        server_name.push_back(pch);  // 保存qos中server名字序列
        pch = strtok(NULL, ",");
        for(int i=0; i<client_name.size(); i++){
            row_data.push_back(stoi(pch));
            pch = strtok(NULL, ",");
        }
        qos.push_back(row_data);
    }else if(format == demand_row_1){  // demand.csv row 1
        char *pch = strtok(str, ",");  // 丢弃字符mtime

        pch = strtok(NULL, ",");
        while(pch != NULL){
            client_name_in_demand.push_back(pch);
            pch = strtok(NULL, ",");
        }
    }else if(format == demand_row_2){  // demand.csv row 2-end
        char *pch = strtok(str, ",");
        T_name.push_back(pch);  // 时刻

        pch = strtok(NULL, ",");
        for(int i=0; i<client_name_in_demand.size(); i++){
            demand[client_name_in_demand[i]].push_back(stoi(pch));
            pch = strtok(NULL, ",");
        }
    }else if(format == site_bandwidth_row_2){  // site_bandwidth.csv row 2-end
        char *pch = strtok(str, ",");
        string name = pch;
        pch = strtok(NULL, ",");
        int value = stoi(pch);

        site_bandwidth[name] = value;
    }else if(format == config_row_2){   // config.ini row 2
        char *pch = strtok(str, "=");
        pch = strtok(NULL, "=");
        qos_constraint = atoi(pch);
    }else{
        printf("Wrong parse for ReadWriteClass::parse_row");
        exit(1);
    }
}

void ReadWriteClass::read_data() {
    string s;

    // 读取文件qos_csv
    cin.clear();
    freopen(qos_csv, "r", stdin);
    // 读取第一行
    cin >> s;  // 第一行找到客户名对应QoS列索引
    parse_row(s, qos_row_1);
    // 读取后面行数
    s = "";
    while(cin >> s) parse_row(s, qos_row_2);

    // 读取文件demand_csv
    cin.clear();
    freopen(demand_csv, "r", stdin);
    // 读取第一行
    cin >> s;
    parse_row(s, demand_row_1);
    // 读取后面行数
    s = "";
    while(cin >> s) parse_row(s, demand_row_2);

    // 读取文件site_bandwidth_csv
    cin.clear();
    freopen(site_bandwidth_csv, "r", stdin);
    // 读取第一行
    cin >> s;  // 第一行丢弃
    // 读取后面行数
    s = "";
    while(cin >> s) parse_row(s, site_bandwidth_row_2);

    // 读取文件config_ini
    cin.clear();
    freopen(config_ini, "r", stdin);
    cin >> s;  // 第一行丢弃
    cin >> s; parse_row(s, config_row_2);
    cin.clear();

    // 节点名称转索引
    for(int i=0; i<client_name.size(); i++){
        client_name2idx[client_name[i]] = i;
    }
    for(int i=0; i<server_name.size(); i++){
        server_name2idx[server_name[i]] = i;
    }
}

void ReadWriteClass::print_data() {
    // demand
    cout << "--------demand---------" << endl;
    for(auto item:demand){
        for(auto val:item.second){
            cout << "key=" << item.first << ", val=" << val << endl;
        }
    }
    cout << endl;
    // site_bandwidth
    cout << "--------site_bandwidth---------" << endl;
    for(auto item:site_bandwidth){
        cout << "key=" << item.first << ", val=" << item.second << endl;
    }
    cout << endl;
    // qos
    cout << "--------qos---------" << endl;
    for(auto item:qos){
        for(auto it:item){
            cout << it << endl;
        }
        cout << endl;
    }
    cout << endl;
    // T_id
    cout << "--------T_id---------" << endl;
    for(auto i:T_name){
        cout << i << " ";
    }
    cout << endl;
    // qos_limit
    cout << "--------qos_limit---------" << endl;
    cout << qos_constraint << endl;
}

void ReadWriteClass::write_answer(vector<string> &ans) {
    FILE *fp = fopen(output_file, "w+");
    for(auto s:ans){
        fprintf(fp, "%s\n", s.c_str());
    }
}
